package com.imooc.authorization.dao;

import com.imooc.authorization.common.BaseDAO;
import com.imooc.authorization.entity.Function;
import com.imooc.authorization.entity.Role;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.List;

/**
 * Created by Administrator on 2016/8/21.
 */

@Repository
public class FunctionDAO extends BaseDAO{

    private class FunctionMapper implements RowMapper<Function> {


        @Override
        public Function mapRow(ResultSet rs, int rowNum) throws SQLException {
            Function function = new Function();
            function.setId(rs.getLong("id"));
            function.setName(rs.getString("name"));
            function.setParentId(rs.getLong("parent_id"));
            function.setSerialNum(rs.getInt("serial_num"));
            function.setUrl(rs.getString("url"));
            function.setAccordion(rs.getInt("accordion"));

            return function;
        }
    }




    /**
     * 保存功能
     * @param function 功能对象
     */
    public void saveFunction(Function function){

        KeyHolder keyHolder=new GeneratedKeyHolder();
        String sql="insert into auth_function(name,parent_id,url,serial_num,accordion) values(?,?,?,?,?)";
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

                PreparedStatement ps=con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1,function.getName());
                ps.setLong(2,function.getParentId());
                ps.setString(3,function.getUrl());
                ps.setInt(4,function.getSerialNum());
                ps.setInt(5,function.getAccordion());


              return ps;
            }
        },keyHolder);

    }

    /**
     * 根据ID理更新功能的URL
     * @param id 功能ID
     * @param url URL
     */
    public void updateUrl(Long id,String url){

        String sql = "update auth_function set url = ? where id = ?";
        jdbcTemplate.update(sql,url,id);

    }

    /**
     * 分页查询功能（根据父节点）
     * @param page 当前页码
     * @param size 每页记录数
     * @param parentId 父节点ID
     * @return 功能集合
     */
    public List<Function> findFunction(int page,int size,Long parentId){

        String sql="select * from auth_function where parent_id=? limit ?,?";



        return jdbcTemplate.query(sql,new Object[]{parentId,(page-1)*size,size},new FunctionMapper());

    }

    /**
     * 根据ID删除功能
     * @param id 功能ID
     */
    public void deleteById(Long id){

        String sql="delete from auth_function where id=?";
        jdbcTemplate.update(sql,id);

    }

    /**
     * 查询全部功能
     * @return 功能集合
     */
    public List<Function> getAllFunctions(){

        return jdbcTemplate.query("select * from auth_function",new FunctionMapper());
    }
}
